#!/usr/local/bin/php -q
<?php
putenv('GDFONTPATH=' . realpath('.'));

include_once '../global-includes.php';
include_once 'region-activities.php';
include_once 'type-activities.php';

$properties = new Properties(PROPERTIES . 'demand-and-supply.properties');

$dbs = &getService('DBService');
$query = 'SELECT adv_type, COUNT(*) FROM search_queries where adv_type !=\'\' group by adv_type';
$sadActivityStat = $dbs->executeQuery($query);

$total = 0;

for($i = 0; $i < count($sadActivityStat); $i++){
	$total += $sadActivityStat[$i][1];	
}

$im = imagecreatefrompng('demand-and-supply-activities-bkg.png');
$orange = imagecolorallocate($im, 252, 100, 4);
$black = imagecolorallocate($im, 0, 0, 0); 

$img_width = 421;
$img_height = 215;
$count = count($properties->properties);

$bar_width = round(($img_width - 49) / $count);

$x1 = 47;
$x2 = $x1 + $bar_width; 
$y2 = 220;

$keyset = array_keys($properties->properties);
for($i = 0; $i < $count; $i++){
	$sadType = $keyset[$i];
	$value = getActivityValue($sadType, &$sadActivityStat, $total);
	
	$bar_height = round($value * 215 / 100);
	$y1 = $y2 - $bar_height;
	
	imagefilledrectangle($im, $x1, $y1, $x2, $y2, $orange);
	if($value > 0){
		imagettftext($im, 8, 0, $x1 + 2, $y1 - 3, $black, 'arial.ttf', $value . '%');
	}	
	imagettftext($im, 8, 270, $x1 + 30, $y2 + 6, $black, 'arial.ttf', win2utf($properties->get($sadType)));
	
	$x1 = $x2 + 2;
	$x2 = $x1 + $bar_width;
}

//header("Content-type: image/png");
imagepng($im, 'demand-and-supply-activities.png');
//imagepng($im);
imagedestroy($im);

?>